package com.example.a2306_shop.address

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.a2306_shop.R
import com.example.a2306_shop.adapter.AddressAdapter
import com.example.a2306_shop.databinding.ActivityAddressBinding
import com.example.lib_base.mvvm.BaseActivity

@Route(path = "/app/AddressActivity")
class AddressActivity : BaseActivity<ActivityAddressBinding,AddressViewModel>() {
    lateinit var addressAdapter: AddressAdapter
    override fun initData() {
        viewModel.addressSuccess.observe(this){
            if(it.code==200){
                addressAdapter.data.clear()
                addressAdapter.data.addAll(it.data.list)
                addressAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun initView() {
        addressAdapter = AddressAdapter()
        binding.rvAddress.adapter = addressAdapter
        binding.rvAddress.layoutManager = LinearLayoutManager(this)
        viewModel.address(10,1)
        //点击新增，跳转到新增界面
        binding.btnAdd.setOnClickListener {
            ARouter.getInstance().build("/app/AddressCreateActivity").navigation()
        }
        //点击item上的控件
        addressAdapter.addChildClickViewIds(R.id.address_cb,R.id.address_edit,R.id.address_delete)
        addressAdapter.setOnItemChildClickListener { adapter, view, position ->
            when(view.id){
                R.id.address_cb->{
                    if(addressAdapter.data[position].isDefault){
                        addressAdapter.data[position].isDefault  = true
                    }else{
                        //调用设置默认地址接口  成功，将列表重新请求
                    }
                }
                R.id.address_edit->{
                    //跳转到编辑界面  地址id 作为参数传过去
                    ARouter.getInstance().build("/app/AddressEditActivity").withInt("addressId",addressAdapter.data[position].id).navigation()
                }
                R.id.address_delete->{}
            }
        }
    }

    override fun getViewModel(): Class<AddressViewModel> = AddressViewModel::class.java

    override fun getLayout(): Int  = R.layout.activity_address

    override fun onResume() {
        super.onResume()
        viewModel.address(10,1)
    }

}